//主控制器
Ext.define("ES.controller.MainController",{ 
    extend:"Ext.app.Controller", 
    models:["ModuleTree"], 
    stores:["SystemInfoStore","ModulesStore"], 
    views:["TopBanner","LeftModuleTree","Footer","DefaultDesktop"], 
    refs:[
          {ref:"CopyRightObj",selector:"viewport > Footer #lbCopyRight"},
          {ref:"MainTabs",selector:"viewport > tabpanel"}
    ], 
    onLaunch:function(){
           Ext.getBody().mask("正在加载数据，请稍后....");
           var infoStore = this.getSystemInfoStoreStore();
            infoStore.loadPage(1,{
                scope: this,
                callback:function(records,operation,success){
                    if(!success){
                         Ext.MessageBox.show({
                            title:"数据加载异常",
                            msg:operation.getError(),
                            icon:Ext.MessageBox.ERROR,
                            buttons:Ext.Msg.OK
                        });
                    }else{
                        var record = records[0];
                        document.title = record.get("SystemName");
                        var obj = this.getCopyRightObj();
                        if(obj){
                            obj.setText(record.get("CopyRightInfo"))
                        }
                    }
                   Ext.getBody().unmask()
                }
            });
    }, 
    init:function(){
            this.control({ 
                    "viewport>LeftModuleTree":{ 
                            itemclick:this.activeTabPanel
                    }
            });
    }, 
    activeTabPanel:function(view, model,item, index, e, opts){
            if(model.data.leaf){
                    var tabId = "tab_" + model.raw.ID;
                     var tabs = this.getMainTabs();
                     if(tabs){
                            var tbsEl = tabs.getEl();
                            tbsEl.mask("正在加载数据，请稍后...");
                             var  tab = tabs.getComponent(tabId);
                             if(!tab){
                                 try{
                                        var uri = model.raw.uri;
                                        if(uri){
                                            var c = this.application.getController(uri+"Controller");
                                            if(c){
                                                c.init();
                                            }
                                        }
                                        var p = {
                                            alias:"widget."+uri +"TabList",
                                            title:model.data.text,
                                            id:tabId,
                                            //layout:"fit",
                                            closable:true
                                        };
                                         var folder = uri.toLowerCase();
                                        var pkg = "ES.view."+folder+"."+uri+"List";
                                        tab = tabs.add(Ext.create(pkg,p));
                                     }catch(e){
                                           tbsEl.unmask();
                                           alert(e);
                                     }
                             }
                             if(tab){
                                  tabs.setActiveTab(tab);
                                  tbsEl.unmask();
                             }else{
                                  tbsEl.unmask();
                                  Ext.Msg.alert("提示", "该功能【"+model.data.text+"】未实现！");
                             }
                     }
            }
    }
})